package com.jservice.api.service.upp;

import com.alibaba.fastjson.JSONObject;
import com.jservice.api.MsgCode;
import com.jservice.api.UppClient;
import com.jservice.api.dto.Request;
import com.jservice.api.dto.Response;
import com.jservice.api.dto.ResponseHeader;
import com.jservice.api.service.BaseService;
import com.jservice.api.service.IService;
import com.jservice.framework.utils.StringUtils;
import com.wondersgroup.upp.api.core.UPPlatformRequest;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Upp2004 - 网点信息查询
 *
 * @Author cwledit
 * @DATE 2017/6/14.
 */
@Service("upp2004")
public class Upp2004 extends BaseService implements IService{

    private String SERVER_NAME = "upp.network.query";


    @Override
    public Response excute(Request request, Response response, ResponseHeader header, String method) throws IOException {
        Map paramMap =  request.getData();
        String display_no = paramsStringFilter(paramMap.get("display_no"));
        String current_page_no = paramsStringFilter(paramMap.get("current_page_no"));
        String network_type = paramsStringFilter(paramMap.get("network_type"));
        String service_type = paramsStringFilter(paramMap.get("service_type"));
        String area = paramsStringFilter(paramMap.get("area"));
        String network_name = paramsStringFilter(paramMap.get("network_name"));

        if (StringUtils.isEmpty(display_no)){
            display_no = "10";
        }
        if (StringUtils.isEmpty(current_page_no)){
            current_page_no = "1";
        }

        Map data = new HashMap();
        data.put("display_no",display_no);
        data.put("current_page_no",current_page_no);
        if (StringUtils.isNotEmpty(network_type)){
            data.put("network_type",network_type);
        }
        if (StringUtils.isNotEmpty(service_type)){
            data.put("service_type",service_type);
        }
        if (StringUtils.isNotEmpty(area)){
            data.put("area",area);
        }
        if (StringUtils.isNotEmpty(network_name)){
            data.put("network_name",network_name);
        }
        UPPlatformRequest uRequest = getUPRequest(SERVER_NAME,request.getHeader());
        uRequest.setBiz_content(sortParam(data));
        UppClient client = UppClient.getInstance();
        JSONObject jsonObject = client.doRequest(uRequest);
        if ("000000".equals(jsonObject.getString("code"))) {
            jsonObject.remove("code");
            return renderSUC(jsonObject,response,header);
        }else{
            return renderFAIL(MsgCode.Code_1009,"市民卡网关返回异常:"+jsonObject.getString("msg"),response,header);
        }
    }
}
